package it.biffi.jirc.test;

import java.io.File;
import java.text.NumberFormat;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

import org.jibble.pircbot.DccFileTransfer;

import it.biffi.jirc.bot.BotClientConfig;
import it.biffi.jirc.bot.BotConnector;
import it.biffi.jirc.bot.BotException;
import it.biffi.jirc.bot.FileTransferBot;
import it.biffi.jirc.bot.FileTransferConfig;
import it.biffi.jirc.bot.IBotConnector;
import it.biffi.jirc.bot.listener.ConnectionListener;
import it.biffi.jirc.bot.listener.FileTransferFinishListener;

public class Demo2 {

	public static void main(String[] args) throws BotException {

		FileTransferBot bot = new FileTransferBot();
		String name = UUID.randomUUID().toString();

		boolean connected = bot.connect("[Bot" + name.substring(0, 5) + "]",
				"irc.crocmax.net", 30000);
		System.out.println("connected: " + connected);
		FileTransferConfig config = new FileTransferConfig();
		config.setSourceChannel("cm-plus");
		config.setSourcePeer("CM|SeRiE|TV|08");
		config.setInputCommand("xdcc send #70");
		bot.start(config);
		System.out.println("bot started on requested channel: "
				+ config.getSourceChannel());
		System.out.println("REQUESTING PACKET");
		final DccFileTransfer transfer = bot.requestPacket(60000,
				new FileTransferFinishListener() {

					@Override
					public void onFinish(Exception finishedWithThisError) {
						if (finishedWithThisError == null) {
							System.out.println("FINISH TRANSFER SUCCESS");
						} else {
							System.out.println("FINISH TRANSFER ERROR");
							finishedWithThisError.printStackTrace();
						}

					}
				});
		if (transfer != null) {
			File file = new File("/Users/Luigi/Downloads/testBot/"
					+ transfer.getFile().getName());
			System.out.println("Receiveing file: " + file.getAbsolutePath());
			System.out.println("Receiveing file: " + file.getAbsolutePath());
			transfer.receive(file, true);

			Timer t = new Timer("PollTrasnferState", true);
			t.scheduleAtFixedRate(new TimerTask() {

				@Override
				public void run() {
					int perc = (int) transfer.getProgressPercentage();
					int rate = (int) transfer.getTransferRate();
					System.out.println("Trasnfer state: " + perc + "%"
							+ "rate: " + rate);
				}
			}, 0, 5000);
		} else {
			System.out.println("Request packet failed");
		}
	}
}
